package com.hjg.baseglide;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.LinkedBlockingQueue;

public class BitmapDispatcher extends Thread {
    private Handler handler = new Handler(Looper.getMainLooper());

    /*队列，线程安全的*/
    private LinkedBlockingQueue<BitmapRequest> bitmapRequestQueue;

    public BitmapDispatcher(LinkedBlockingQueue<BitmapRequest> bitmapRequestQueue) {
        this.bitmapRequestQueue = bitmapRequestQueue;
    }

    @Override
    public void run() {
        super.run();
        //只要线程开始，就会一直循环取加载请求，这个是一个应用正常的开销
        while (!isInterrupted()) {
            //先从队列中去取一次加载需求

            try {
                BitmapRequest bitmapRequest = bitmapRequestQueue.take();
                //设置一个展位图
                showPlaceHolder(bitmapRequest);
                //去查找bitmap
                Bitmap bitmap = findBitmap(bitmapRequest);
                //展示bitmap
                showBitmap(bitmapRequest, bitmap);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


        }
    }


    /**
     * 展示bitmap
     *
     * @param bitmapRequest
     * @param bitmap
     */
    private void showBitmap(BitmapRequest bitmapRequest, Bitmap bitmap) {
        //然后将下载好的bitmap设置到对应的imageView中
        if (bitmap != null &&
                bitmapRequest.getMd5().equals(bitmapRequest.getImageView().getTag())) {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    bitmapRequest.getImageView().setImageBitmap(bitmap);
                }
            });
        }
    }


    /**
     * 获取bitmap，可能是从内存中可能是从文件中，最后才是在网络中
     *
     * @param bitmapRequest
     * @return
     */
    private Bitmap findBitmap(BitmapRequest bitmapRequest) {
        //网络下载
        Bitmap bitmap = BitmapFromNet(bitmapRequest.getUrl());

        return bitmap;
    }


    /**
     * 网络下载bitmap
     *
     * @param uri
     * @return
     */
    private Bitmap BitmapFromNet(String uri) {
        InputStream inputStream = null;
        try {
            URL url = new URL(uri);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            inputStream = httpURLConnection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }


        return null;
    }


    /**
     * 加载展位图
     *
     * @param bitmapRequest
     */
    private void showPlaceHolder(BitmapRequest bitmapRequest) {
        Drawable drawable = bitmapRequest.getDrawable();
        if (drawable == null) {
            return;
        }
        ImageView imageView = bitmapRequest.getImageView();
        //非主线程，这里需要切换线程
        handler.post(new Runnable() {
            @Override
            public void run() {
                imageView.setBackground(drawable);
            }
        });
    }
}
